<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_school_years extends MY_Model {
	
	protected $__table = 'school_years';
	protected $_table = 'school_years';
	protected $_uid = 'id';
	protected $_timestamp = false;
	
	public function get_all_school_year($id=FALSE)
	{
		if($id == FALSE){
			$query = $this->db->get($this->__table);
		}else{
			$query = $this->db->where('id',$id)->get($this->__table);
		}
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	/*
		Get Set School Year by the user if null get the system set school year
	*/
	public function get_set_school_year()
	{
		return $this->get_sy(true);
	}

	public function get_school_year_set()
	{
		
		$query = $this->db->where('is_set','yes')->limit(1)->get($this->__table);
		
		return $query->num_rows() >= 1 ? $query->row() : FALSE;
	}
	
	public function get_current_sy_format()
	{
		$data = $this->c_sy;
		if(!empty($data))
		{
			$date = $data->sy_from.' - '.$data->sy_to;
			return $date;
		}else{
			return NULL;
		}
	}

	public function get_current_sy_desc()
	{
		$data = $this->c_sy;
		if(!empty($data))
		{
			return $data->desc;
		}

		return NULL;
	}

	public function get_available_sy()
	{
		$this->load->model('M_user_open_sy');
		$ct = $this->M_user_open_sy->get_user_profile($this->c_sy->id);

		unset($get);
		if($ct){
			$get['where']['id <> '] = $ct->id;
		}
		$get['order'] = "sy_from";
		$get['limit'] = 5;
		return $this->get_record('school_years', $get);
	}

	/*
	 * Get Current School Year Notice
	 * Check if System Current School Year is different from the user current school year
	 * @return string 
	 */
	public function get_current_sy_notice()
	{
		$sys_sy = $this->get_school_year_set();
		$user_sy = $this->c_sy;


		if($user_sy && $sys_sy){
			if($user_sy->id !== $sys_sy->id){
				return "Your current is school year is not ";
			}
		}

		return false;

		
		return false;
	}
	
	/*
		Add School Year
	*/
	public function add_school_year($input)
	{
		$this->db->insert($this->__table,$input);
		return $this->db->affected_rows() > 0 ? array('status'=>'true') : array('status'=>'false');

		// if($this->verify_data($input,1) == FALSE)
		// {
		// 	$this->db->insert($this->__table,$input);
		// 	return $this->db->affected_rows() > 0 ? array('status'=>'true') : array('status'=>'false');
		// }else
		// {
		// 	return array('status'=>'exist');
		// }
	}
	
	/*
		query to count all school years for pagnation
	*/	
	public function count_school_year()
	{
		return $this->db->count_all($this->__table);
	}
	
	/*
		query to fetch all school year for pagnation
	*/	
	public function fetch_school_year($limit, $start)
	{
        $this->db->limit($limit, $start);
        $query = $this->db->select(array('id','sy_from','sy_to','is_set','desc','open_portal_reg'))->get($this->__table);
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	/*
		set School Year
	*/
	public function set_school_year($input,$id)
	{
		$input['is_set'] = '';
		$this->db->set($input)->update($this->__table);
		$input['is_set'] = 'yes';
		$this->db->set($input)->where('id',$id)->update($this->__table);
		return $this->db->affected_rows() > 0 ? array('status'=>'true') : array('status'=>'false');			
	}
	
	public function get_id_of_current_sy()
	{
		if($sy = $this->c_sy){
			return $sy->id;
		}

		return false;
	}
	
	/* verify_data
	 * @param array
	 * @param int
	*  verifies if data entered by user is existing already in the database
	*  $strict level 1,2
	*  level 1 = if one field has existing data in database returns true else false
	*  level 2 = if all field has existing data in database returns true else false
	*  12/11/2012
	*/
	public function verify_data($data,$strict_level)
	{		
			if($strict_level == 1)
			{
				$query = $this->db->or_where($data)->get($this->__table);
			}elseif($strict_level == 2){
				$query = $this->db->where($data)->get($this->__table);
			}
			return $query->num_rows() > 0 ? TRUE : FALSE;
	}
	
	public function get_all_school_year_array($all = FALSE)
	{
		$query = $this->db->get($this->__table);
		if($query->num_rows() >= 1 )
		{
			$result[''] = 'Choose Academic year';
			if($all == TRUE)
			{
				$result[0] = 'All Academic Years';
			}
			
			foreach($query->result() as $y)
			{
				$result[$y->id] = $y->sy_from.' - '.$y->sy_to;
			}
			return $result;
		}else{
			return FALSE;
		}
	}
	
	public function get_less_than_sy()
	{
		$csy = $this->c_sy->id;
		$this->db->where('id <', $csy); 
		$query = $this->db->get($this->__table);

		if($query->num_rows() >= 1 )
		{
			$result[''] = 'Choose Academic year';
			
			foreach($query->result() as $y)
			{
				if($y->desc==''){
					$result[$y->id] = $y->sy_from.' - '.$y->sy_to;
				}else{
					$result[$y->id] = $y->sy_from.' - '.$y->sy_to.' ( '.$y->desc.' )';
				}
			}
			return $result;
		}else{
			return FALSE;
		}
	}
	
	/**
	 * Open or close parent registration in portal page
	 * @param int $status 1/0
	 * @param int $sy_id School_years id
	 * @return boolean true/false
	 */
	public function set_open_reg($status, $sy_id)
	{

		//Update All to Zero - only 1 record should be open
		$this->db->set('open_portal_reg','0');
		$this->db->update($this->_table);

		if($status == 1){
			$this->db->set('open_portal_reg','1');
			$this->db->where('id',$sy_id);
			$this->db->update($this->_table);
		}

		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function open_reg_sy()
	{
		//$q = $this->db->select('id')->where('is_set','yes')->where('open_portal_reg',1)->get($this->_table);
		$q = $this->db->select(array('sy_from','sy_to'))->where('open_portal_reg',1)->get($this->_table);
		return $q->num_rows() > 0 ? $q->row() : FALSE;
	}
}
?>